<?php 
include './DataJson.class.php';
include './functions.php';
$data = $_POST;
// var_dump($data);exit;
$path='./db/user.json';
$oldData = DataJson::readJson($path);
// $usernamePreg='//'

if(empty($data['username'])){
	$error = 0;
}elseif (empty($data['email'])) {
	$error = 1;
}elseif (empty($data['password'])) {
	$error = 2;
}elseif ($data['password']!=$data['passwordConfirm']) {
	echo json_encode(['error'=>1,'info'=>"两次输入的密码不一样"]);exit;
}elseif (!preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", 
trim($data['username']))){
	$error = 4;
}elseif (deep_in_array($data['username'], $oldData)) {
	$error = 5;
}elseif (deep_in_array($data['email'], $oldData)) {
	$error = 6;
}else{
	echo json_encode(['error'=>0,'info'=>"注册成功"]);
	$data['signUpTime']=time();
	$data['password']=md5($data['password']);
	// $data=array_splice($data,4,1);
	unset($data['passwordConfirm']);
	// array_splice($data, $id,1);
	$oldData[]=$data;
	DataJson::writeJson($oldData,$path);
	session_start();
	$_SESSION["username"]=$data['username'];
	$_SESSION["loginSuccess"]=true;
	header("Location:./index.php");
}

 ?>
<!-- 
 <!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">
		var  error = <?php echo $error ?>;
		switch(error){
			case 0:
				if(confirm("用户名不能为空")){
					location.href = "./index.php";
				}break;
			case 1:
				if(confirm("邮箱不能为空")){
					location.href = "./index.php";
				}break; 
			case 2:
				if(confirm("密码不能为空")){
					location.href = "./index.php";
				}break;
			case 3:
				if(confirm("两次输入的密码不一样")){
					location.href = "./index.php";
				}break;
			case 4:
				if(confirm("输入的用户名不合法")){
					location.href = "./index.php";
				}break;
			case 5:
				if(confirm("用户名已存在")){
					location.href = "./index.php";
				}break;
			case 6:
				if(confirm("邮箱已存在")){
					location.href = "./index.php";
				}break;
			case 100:
				if(confirm("注册成功!")){
					location.href = "./index.php";
				}break;
		}
		
	</script>
</head>
<body>
	
</body>
</html> -->